<?php

$name = array(
	'name'	=> 'name',
	'id'	=> 'name',
	'value' => set_value('name') != ""?set_value('name'):(isset($category_name))?$category_name:"",
	'class' => "txtbox-long"
);

$url = array(
	'name'	=> 'url',
	'id'	=> 'url',
	'value' => set_value('url') != ""?set_value('name'):(isset($category_url_name))?$category_url_name:"",
	'class' => "txtbox-long"
);

?>

<script type="text/javascript">
	$(document).ready(function(){
		<?php if(!isset($category_id)): ?>
		$("input[name=name]").keyup(function(){
			$("input[name=url]").val(renameUrl($(this).val()));
		})
		<?php endif; ?>
	})
</script>

<div id="content-top">
	<h2>Categorias</h2>
	<a id="topLink" href="#">Change Order</a> 
	<span class="clearFix"> </span>
</div><!-- end of div#content-top -->

<div id="left-col">
 	
 	<?php $this->load->view($template_path ."pages/categories/commons/menu");?>
	
 </div><!-- end of #left-col -->
 
 <div id="mid-col" class="full-col">
 	
 	<?php if (validation_errors()): ?>
		<div class="error">
		<?php echo validation_errors(); ?>
		</div>
	<?php endif; ?>

 	<div class="box">      
      	<h4 class="white">Form Elements</h4>
        <div class="box-container">
        	<?php echo form_open($url_form, array("class" => "middle-forms")); ?>
        	
      		<h3>Añadir categoria</h3>
      		<p>Please complete the form below. Mandatory fields marked <em>*</em></p>
      		<fieldset>
      			<legend>Fieldset Title</legend>
      			<ol>
					
					<li>
      					<label class="field-title">Categoria padre:</label> 
      					<label>
		      				<select class="form-select" name="parent_id">
		      					<option value="-1">-</option>
		      					<?php categoriasRecursivas($categorias, "", $category_parent_id); ?>
		      					
				 				<?php 
				 				//@TODO Problema con seleccionar en la edicion... selected
				 				/*
				 				$this->load->helper('generate_list');
								
								$options = array('level'=>0,'indent'=>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|');
								$template = '<option value="{ID}">{INDENT}- {NAME}</option>{SUBS}'; 
								
								echo generate_list($categorias, $options, $template); 
				 				*/
				 				?>
							</select>
						</label>
						<span class="clearFix"> </span>
					</li>
      				
      				<li class="even">
      					<label class="field-title">Nombre:</label> 
      					<label><?php echo form_input($name); ?></label>
      					<span class="clearFix"> </span>
      				</li>
      				
      				<li>
      					<label class="field-title">Url:</label> 
      					<label><?php echo form_input($url); ?></label>
      					<span class="clearFix"> </span>
      				</li>
      				
      			</ol><!-- end of form elements -->
      		</fieldset>
      		<p class="align-right"><input type="image" src="<?php echo static_url("images/bt-send-form.gif"); ?>"/></p>
      		<span class="clearFix"> </span>
      		<?php echo form_close(); ?>
      	</div>
      	</div>
 
</div><!-- end of #mid-col --> 

<?php 

function categoriasRecursivas($categorias, $separador = "", $selectedCategory = NULL){
	if($categorias){
 		foreach($categorias as $categoria){
			$selected = "";
			if(isset($selectedCategory) && $selectedCategory != NULL && $selectedCategory == $categoria->id){
				$selected = "selected='selected'";
			}
 			?>
 			<option value="<?php echo $categoria->id;?>" url="<?php echo $categoria->url_name;?>" <?php echo $selected;?>><?php echo $separador . $categoria->name;?></option> 
 			<?php 
 			if(isset($categoria->categories) && count($categoria->categories)>0){
 				categoriasRecursivas($categoria->categories, $separador . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $selectedCategory);
 			}
 		}
	}
}

?>